package com.crm.CLdriving.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.dto.req.LoginReqDto;
import com.crm.CLdriving.dto.resp.LoginRespDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.LoginMapper;
import com.crm.CLdriving.po.LoginPO;
import com.crm.CLdriving.service.LoginService;
import com.crm.CLdriving.util.token.TokenUtils;

import lombok.extern.log4j.Log4j2;

@Transactional
@Service
@Log4j2
public class LoginServiceImpl implements LoginService {

	@Autowired
	private LoginMapper loginMapper;
	
	@Override
	public BaseResponse<LoginRespDto> login(LoginReqDto loginReqDto) {
		
		LoginPO loginPO = new LoginPO();
		loginPO.setLoginUserName(loginReqDto.getLoginUserName());
		loginPO.setLoginPassWord(loginReqDto.getLoginPassWord());
		LoginPO login = loginMapper.selectOne(loginPO);			
		LoginRespDto response = new LoginRespDto();
		if (login != null) {
			log.info("登录查询返回数据"+login.toString());			
			String token = TokenUtils.getToken(login.toString());
			response.setLoginUserName(login.getLoginUserName());
			response.setLoginPassWord(login.getLoginPassWord());
			response.setToken(token);		
			return BaseResponse.successOf(response);
		}				
		return BaseResponse.message(ResponseEnum.LOGINFAIL);
	}

}
